﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Configuration;
using System.Threading;

namespace OAHelper
{
    public partial class OALogin : Form
    {
        public OALogin()
        {
            InitializeComponent();
        }
        //定义基本操作地址
        public static readonly string OA_LOGIN_URL = ConfigurationManager.AppSettings["OA_LOGIN_URL"].ToString();
        //private readonly string OA_LOGIN_URL = "http://zmb-pc:8080/bmpt";
        public static readonly string OA_SIGNIN_MORNING_URL = ConfigurationManager.AppSettings["OA_SIGNIN_MORNING_URL"].ToString();
        public static readonly string OA_SIGNIN_NOON_URL = ConfigurationManager.AppSettings["OA_SIGNIN_NOON_URL"].ToString();
        public static readonly string OA_SIGNIN_AFTERNOON_URL = ConfigurationManager.AppSettings["OA_SIGNIN_AFTERNOON_URL"].ToString();
        public static readonly string OA_SIGNIN_NIGHT_URL = ConfigurationManager.AppSettings["OA_SIGNIN_NIGHT_URL"].ToString();
        public static readonly string OA_COUNT_URL = ConfigurationManager.AppSettings["OA_COUNT_URL"].ToString();
        public static readonly string OA_TASK_URL = ConfigurationManager.AppSettings["OA_TASK_URL"].ToString();
        public static readonly string OA_MYSIGNIN_URL = ConfigurationManager.AppSettings["OA_MYSIGNIN_URL"].ToString();

        public static string OACookies = null;
        public static string name = null;
        public static string pass = null;
        public static string paramData = null;
        public HttpCore.HttpCore httpcore = new HttpCore.HttpCore();
        public HttpCore.AsyncHttp httpasync = new HttpCore.AsyncHttp();
        public delegate string OALoginCookiesDelegate(string _url, string paramData, Encoding encoding);
        private void btnLogin_Click(object sender, EventArgs e)
        {
            BeginLoginCookie();
        }
        /// <summary>
        /// 开始登录
        /// </summary>
        private void BeginLoginCookie()
        {
            tsslblTime.Text = "正在登录中，请等待……";
            btnLogin.Enabled = false;
            name = txtName.Text.Trim();
            pass = txtPass.Text.Trim();
            paramData = "UNAME=" + name + "&PASSWORD=" + pass;
            this.rtxtDebug.Clear();
            string msg = httpasync.GetLoginCookies(OA_LOGIN_URL, paramData, Encoding.Default);
            this.rtxtDebug.AppendText(msg);
            if (msg != "")
            {
                OACookies = msg;//写入Cookie拿到权限
                tsslblTime.Text = "登录成功……";
                //TODO：按lua脚本执行签到操作，并进行随机时间抽取签到！
                OpenSignInUI();//拿到Cookie开始执行后续操作。

            }
            else
            {
                //MessageBox.Show("登录失败，用户名或密码不正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tsslblTime.Text = "登录失败，用户名或密码不正确！";
                txtName.Focus();
                txtName.SelectAll();
                btnLogin.Enabled = true;
            }

        }
        //执行后续操作
        private void OpenSignInUI()
        {
            SignIn signin = new SignIn();
            signin.Show();
            this.Hide();
        }
        private void ckbDebug_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ckbDebug.Checked)
            {
                this.Height = 444;
            }
            else
            {
                this.Height = 244;
            }
        }

        private void OALogin_Load(object sender, EventArgs e)
        {
            txtName.Text = ConfigurationManager.AppSettings["USERNAME"].ToString();
            txtPass.Text = ConfigurationManager.AppSettings["USERPASSWORD"].ToString();
            rtxtDebug.AppendText(ConfigurationManager.AppSettings["VERSION"].ToString() + "\n");
            //bool auto = bool.Parse(ConfigurationManager.AppSettings["AUTOLOGIN"].ToString());
            //if (auto)
            //{
            //    btnLogin.PerformClick();
            //}
            ckbDebug.Checked = false;

        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                BeginLoginCookie();
            }
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.txtPass.Focus();
                this.txtPass.SelectAll();
            }
        }
    }
}
